// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;

namespace SnapObjects.Data
{
    internal abstract class SqlJoinOnAndOr<TCondition> : SqlBuilderComb, ISqlJoinOnAndOr<TCondition>
    {
        protected SqlBuildClause _clause;
        protected JoinElement _current;

        public SqlJoinOnAndOr(ISqlBuilder builder, SqlBuildClause join) : base(builder)
        {
            _clause = join;
        }

        protected abstract TCondition BuilderResult();

        /// <summary>
        ///     Gets whether any search condition has been added to the ON clause using the On or OnRaw method and so on.
        /// </summary>
        public bool HasOn =>
            _clause.TryGet(out JoinElement element) && element.On != null;

        private TCondition JoinOnCondition(ConditionElement condition, SqlStitchingType stitchingType)
        {
            if (_current != null)
            {
                _clause.Changed();

                if (_current.On != null)
                {
                    _current.On.AndOr.Add(condition, stitchingType);
                }
                else
                {
                    _current.On = condition;
                }
            }
            else
            {
                throw new InvalidOperationException("Please call 'Join' before adding conditions.");
            }

            return this.BuilderResult();
        }

        /// <summary>
        ///     Adds the AND logical operator and a search condition to the ON clause; and specifies a SQL expression on the 
        ///     left of the operator and a SQL parameter on the right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="parameter">
        ///     An ISqlParameter object which represents a SQL parameter on the right of the operator.
        ///     It can be created by calling the SqlBuilder.Parameter method.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public TCondition AndOn(string left, ISqlParameter parameter)
        {
            return this.AndOn(left, SqlBinaryOperator.Equals, parameter);
        }

        /// <summary>
        ///     Adds the AND logical operator and a search condition to the ON clause; and specifies a SQL expression on
        ///     the left of the operator and a SQL parameter on the right of the operator.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.
        /// </param>
        /// <param name="parameter">
        ///     An ISqlParameter object which represents a SQL parameter on the right of the operator.
        ///     It can be created by calling the SqlBuilder.Parameter method.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public TCondition AndOn(string left, SqlBinaryOperator sqlOperator, ISqlParameter parameter)
        {
            _clause.AddParameter(parameter);

            return this.JoinOnCondition(
                new ConditionElement(left, SqlOperatorConverter.ToSqlOperator(sqlOperator), parameter, SqlRightType.Parameter),
                SqlStitchingType.And);
        }

        /// <summary>
        ///     Adds the AND logical operator and a search condition to the ON clause; and specifies SQL expressions 
        ///     on both the left and right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="right">A SQL expression on the right of the operator.</param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public TCondition AndOn(string left, string right)
        {
            return this.AndOn(left, SqlBinaryOperator.Equals, right);
        }

        /// <summary>
        ///     Adds the AND logical operator and a search condition to the ON clause; and specifies SQL expressions
        ///     on both the left and right of the operator.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.
        /// </param>
        /// <param name="right">A SQL expression on the right of the operator.</param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public TCondition AndOn(string left, SqlBinaryOperator sqlOperator, string right)
        {
            return this.JoinOnCondition(
                new ConditionElement(left, SqlOperatorConverter.ToSqlOperator(sqlOperator), right, SqlRightType.Clause),
                SqlStitchingType.And);
        }

        /// <summary>
        ///     Adds the AND logical operator and a search condition to the ON clause; and specifies a SQL expression on 
        ///     the left of the operator and a SQL subquery on the right. The operator is '='.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlBuilder">
        ///     An ISqlBuilder object which represents a SQL subquery on the right of the operator.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public TCondition AndOn(string left, ISqlBuilder sqlBuilder)
        {
            return this.AndOn(left, SqlBinaryOperator.Equals, sqlBuilder);
        }

        /// <summary>
        ///     Adds the AND logical operator and a search condition to the ON clause; and specifies a SQL expression on 
        ///     the left of the operator and a SQL subquery on the right.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.
        /// </param>
        /// <param name="sqlBuilder">
        ///     An ISqlBuilder object which represents a SQL subquery on the right of the operator.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public TCondition AndOn(string left, SqlBinaryOperator sqlOperator, ISqlBuilder sqlBuilder)
        {
            var builder = (SqlBuilderBase)sqlBuilder;

            _clause.AddParameters(builder.Statement.Parameters);

            return this.JoinOnCondition(
                new ConditionElement(left, SqlOperatorConverter.ToSqlOperator(sqlOperator), builder.Statement, SqlRightType.Statement),
                SqlStitchingType.And);
        }

        /// <summary>
        ///     Adds the AND logical operator and a search condition (specified by an ISqlJoinCondition object) to the ON clause.
        /// </summary>
        /// <param name="condition">
        ///     An ISqlJoinCondition object which represents a search condition in the ON clause.
        ///     This object can be created by using the SqlBuilder.On method and so on.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public TCondition AndOn(ISqlJoinCondition condition)
        {
            var hc = (SqlJoinCondition)condition;

            if (hc.Clause.TryGet(out WhereElement whereElement))
            {
                this.JoinOnCondition(whereElement.Condition, SqlStitchingType.And);

                _clause.AddParameters(hc.BuilderParameters);
            }

            return this.BuilderResult();
        }

        /// <summary>
        ///     Adds the AND logical operator and a raw SQL to the ON clause.
        /// </summary>
        /// <param name="clause">A string of raw SQL to be added to the ON clause.</param>
        /// <param name="parameters">
        ///     (Optional) An array of ISqlParameter objects which define the SQL parameters used in the raw SQL.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public TCondition AndOnRaw(string clause, params ISqlParameter[] parameters)
        {
            _clause.AddParameters(parameters);

            return this.JoinOnCondition(
                new ConditionElement(null, SqlOperator.Equals, clause, SqlRightType.Clause),
                SqlStitchingType.And);
        }

        /// <summary>
        ///     Adds the AND logical operator and a search condition to the ON clause; and specifies a SQL expression on 
        ///     the left the operator and a specific value on the right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">An expression on the left of the operator.</param>
        /// <param name="value">A specific value on the right of the operator.</param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public TCondition AndOnValue(string left, object value)
        {
            return this.AndOnValue(left, SqlBinaryOperator.Equals, value);
        }

        /// <summary>
        ///     Adds the AND logical operator and a search condition to the ON clause; and specifies a SQL expression on 
        ///     the left the operator and a specific value on the right of the operator.
        /// </summary>
        /// <param name="left">An expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the expression on the left and
        ///     the value on the right.
        /// </param>
        /// <param name="value">A specific value on the right of the operator.</param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public TCondition AndOnValue(string left, SqlBinaryOperator sqlOperator, object value)
        {
            return this.JoinOnCondition(
                new ConditionElement(left, SqlOperatorConverter.ToSqlOperator(sqlOperator), value, SqlRightType.Value),
                SqlStitchingType.And);
        }

        /// <summary>
        ///     Adds the OR logical operator and a search condition to the ON clause; and specifies a SQL expression on
        ///     the left of the operator and a SQL parameter on the right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="parameter">
        ///     An ISqlParameter object which represents a SQL parameter on the right of the operator.
        ///     It can be created by calling the SqlBuilder.Parameter method.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public TCondition OrOn(string left, ISqlParameter parameter)
        {
            return this.OrOn(left, SqlBinaryOperator.Equals, parameter);
        }

        /// <summary>
        ///     Adds the OR logical operator and a search condition to the ON clause; and specifies a SQL expression on
        ///     the left of the operator and a SQL parameter on the right of the operator.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.
        /// </param>
        /// <param name="parameter">
        ///     An ISqlParameter object which represents a SQL parameter on the right of the operator.
        ///     It can be created by calling the SqlBuilder.Parameter method.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public TCondition OrOn(string left, SqlBinaryOperator sqlOperator, ISqlParameter parameter)
        {
            _clause.AddParameter(parameter);

            return this.JoinOnCondition(
                new ConditionElement(left, SqlOperatorConverter.ToSqlOperator(sqlOperator), parameter, SqlRightType.Parameter),
                SqlStitchingType.Or);
        }

        /// <summary>
        ///     Adds the OR logical operator and a search condition to the ON clause; and specifies SQL expressions on
        ///     both the left and right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="right">A SQL expression on the right of the operator.</param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public TCondition OrOn(string left, string right)
        {
            return this.OrOn(left, SqlBinaryOperator.Equals, right);
        }

        /// <summary>
        ///     Adds the OR logical operator and a search condition to the ON clause; and specifies SQL expressions on
        ///     both the left and right of the operator.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.
        /// </param>
        /// <param name="right">A SQL expression on the right of the operator.</param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public TCondition OrOn(string left, SqlBinaryOperator sqlOperator, string right)
        {
            return this.JoinOnCondition(
                new ConditionElement(left, SqlOperatorConverter.ToSqlOperator(sqlOperator), right, SqlRightType.Clause),
                SqlStitchingType.Or);
        }

        /// <summary>
        ///     Adds the OR logical operator and a search condition to the ON clause; and specifies a SQL expression on the 
        ///     left of the operator and a SQL subquery on the right. The operator is '='.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlBuilder">
        ///     An ISqlBuilder object which represents a SQL subquery on the right of the operator.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public TCondition OrOn(string left, ISqlBuilder sqlBuilder)
        {
            return this.OrOn(left, SqlBinaryOperator.Equals, sqlBuilder);
        }

        /// <summary>
        ///     Adds the OR logical operator and a search condition to the ON clause; and specifies a SQL expression on the
        ///     left of the operator and a SQL subquery on the right.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.
        /// </param>
        /// <param name="sqlBuilder">
        ///     An ISqlBuilder object which represents a SQL subquery on the right of the operator.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public TCondition OrOn(string left, SqlBinaryOperator sqlOperator, ISqlBuilder sqlBuilder)
        {
            var builder = (SqlBuilderBase)sqlBuilder;

            _clause.AddParameters(builder.Statement.Parameters);

            return this.JoinOnCondition(
                new ConditionElement(left, SqlOperatorConverter.ToSqlOperator(sqlOperator), builder.Statement, SqlRightType.Statement),
                SqlStitchingType.Or);
        }

        /// <summary>
        ///     Adds the OR logical operator and a search condition (specified by an ISqlJoinCondition object) to the ON clause.
        /// </summary>
        /// <param name="condition">
        ///     An ISqlJoinCondition object which represents a search condition in the ON Clause.
        ///     This object can be created by using the SqlBuilder.On method and so on.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public TCondition OrOn(ISqlJoinCondition condition)
        {
            var hc = (SqlJoinCondition)condition;

            if (hc.Clause.TryGet(out WhereElement whereElement))
            {
                this.JoinOnCondition(whereElement.Condition, SqlStitchingType.Or);

                _clause.AddParameters(hc.BuilderParameters);
            }

            return this.BuilderResult();
        }

        /// <summary>
        ///     Adds the OR logical operator and a raw SQL to the ON clause.
        /// </summary>
        /// <param name="clause">A string of raw SQL to be added to the ON clause.</param>
        /// <param name="parameters">
        ///     (Optional) An array of ISqlParameter objects which define the SQL parameters used in the raw SQL.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public TCondition OrOnRaw(string clause, params ISqlParameter[] parameters)
        {
            _clause.AddParameters(parameters);

            return this.JoinOnCondition(
                new ConditionElement(null, SqlOperator.Equals, clause, SqlRightType.Clause),
                SqlStitchingType.Or);
        }

        /// <summary>
        ///     Adds the OR logical operator and a search condition to the ON clause; and specifies a SQL expression on 
        ///     the left of the operator and a specific value on the right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">An expression on the left of the operator.</param>
        /// <param name="value">
        ///     A specific value on the right of the operator.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public TCondition OrOnValue(string left, object value)
        {
            return this.OrOnValue(left, SqlBinaryOperator.Equals, value);
        }

        /// <summary>
        ///     Adds the OR logical operator and a search condition to the ON clause; and specifies a SQL expression on 
        ///     the left of the operator and a specific value on the right of the operator.
        /// </summary>
        /// <param name="left">An expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the expression on the left
        ///     and the value on the right.
        /// </param>
        /// <param name="value">A specific value on the right of the operator.</param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public TCondition OrOnValue(string left, SqlBinaryOperator sqlOperator, object value)
        {
            return this.JoinOnCondition(
                new ConditionElement(left, SqlOperatorConverter.ToSqlOperator(sqlOperator), value, SqlRightType.Value),
                SqlStitchingType.Or);
        }
    }
}
